<template>
  <div>
    <h1>商品审核</h1>
    <!-- 条件搜索 -->
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入商品名称/ID/关键词"
            clearable
          />
        </el-form-item>
        <el-form-item label="商品状态">
          <el-select
            v-model="searchForm.statusId"
            placeholder="请选择状态"
            clearable
          >
            <el-option label="草稿" :value="0" />
            <el-option label="待审核" :value="1" />
            <el-option label="已上线" :value="2" />
            <el-option label="已下线" :value="3" />
            <el-option label="缺货" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadProdList()">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格数据 -->
    <div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="prodId" label="ID" width="40" />
        <el-table-column prop="prodSpu" label="SPU" width="60" />
        <el-table-column prop="prodName" label="商品名称" width="100" >
            <template #default="scope">
                <span v-html="scope.row.prodName" />
            </template>
        </el-table-column>
        <el-table-column prop="kindName" label="商品种类" width="80" />
        <el-table-column prop="typeName" label="商品分类" width="80" />
        <el-table-column prop="brandName" label="品牌" width="60" >
            <template #default="scope">
                <span v-html="scope.row.brandName" />
            </template>
        </el-table-column>
        <el-table-column prop="unitName" label="单位" width="60" />
        <el-table-column prop="tagName" label="标签" width="80" />
        <el-table-column prop="appr" label="供应商" width="80" />
        <el-table-column prop="prodImage" label="图片" width="60">
          <template #default="scope">
            <el-image
              :src="scope.row.prodImage"
              style="width: 50px; height: 50px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="prodStatus" label="状态" width="70">
          <template #default="scope">
            <span v-if="scope.row.prodStatus == 0">草稿</span>
            <span v-if="scope.row.prodStatus == 1">待审核</span>
            <span v-if="scope.row.prodStatus == 2">已上线</span>
            <span v-if="scope.row.prodStatus == 3">已下线</span>
            <span v-if="scope.row.prodStatus == 4">缺货</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column label="操作" />
      </el-table>
    </div>
    <!-- 分页 -->
    <div>
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getProdFromEs } from "../api/api";

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
  statusId: null,
});

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadProdList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadProdList();
};

const loadProdList = () => {
  getProdFromEs(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadProdList();
});
</script>

<!-- 分页样式 -->
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
<!-- 条件搜索样式 -->
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
